//
//  RCRecommendFoodListDataModel.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-21.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCRecommendFoodListDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NSArray+EX.h"

@implementation RCRecommendFoodTypeItem

@synthesize typeID = _typeID;
@synthesize typeName = _typeName;

-(id)init{
    self = [super init];
    if (self) {
        _typeID = nil;
        _typeName = nil;
    }
    return self;
}

-(void)dealloc{
    self.typeID = nil;
    self.typeName = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCRecommendFoodTypeItem *retObject = nil;
    retObject = [[RCRecommendFoodTypeItem alloc]init];
    if (nil == retObject) {
        return nil;
    }
    retObject.typeID = [data stringValueForKey:@"id" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.typeName = [data stringValueForKey:@"name" defaultValue:nil operation:NSStringOperationTypeTrim];
    return [retObject autorelease];
}

@end

@implementation RCRecommendFoodTypeDataModel

@synthesize typeItemList = _typeItemList;

-(id)init{
    self = [super init];
    if (self) {
        _typeItemList = nil;
    }
    return self;
}

-(void)dealloc{
    self.typeItemList = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    
    NSArray *dictArray = [data arrayValueForKey:@"data" defaultValue:nil];
    if (nil == dictArray) {
        return nil;
    }
    RCRecommendFoodTypeDataModel *retObject = nil;
    retObject = [[RCRecommendFoodTypeDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:[dictArray count]];
    for (int i = 0; i < [dictArray count]; i++) {
        NSDictionary *itemDict = [dictArray safeObjectAtIndex:i];
        if (nil == itemDict) {
            continue;
        }
        RCRecommendFoodTypeItem *item = [RCRecommendFoodTypeItem parseDataFromJSON:itemDict];
        if (nil == item) {
            continue;
        }
        [tempArray addObject:item];
    }
    retObject.typeItemList = [NSArray arrayWithArray:tempArray];
    [tempArray release];
    return [retObject autorelease];
}

@end

@implementation RCRecommendFoodListItem

@synthesize foodID = _foodID;
@synthesize foodName = _foodName;
@synthesize foodPicUrl = _foodPicUrl;
@synthesize foodScore = _foodScore;
@synthesize foodAddress = _foodAddress;
@synthesize foodPrice = _foodPrice;
@synthesize foodDesc = _foodDesc;
@synthesize foodDistance = _foodDistance;

-(id)init{
    self = [super init];
    if (self) {
        _foodID = nil;
        _foodName = nil;
        _foodPicUrl = nil;
        _foodAddress = nil;
        _foodDesc = nil;
        _foodDistance = nil;
        _foodScore = 0;
        _foodPrice = 0;
    }
    return self;
}

-(void)dealloc{
    self.foodID = nil;
    self.foodName = nil;
    self.foodPicUrl = nil;
    self.foodAddress = nil;
    self.foodDesc = nil;
    self.foodDistance = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCRecommendFoodListItem *retObject = nil;
    retObject = [[RCRecommendFoodListItem alloc]init];
    if (nil == retObject) {
        return nil;
    }
    retObject.foodID = [data stringValueForKey:@"id" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.foodName = [data stringValueForKey:@"name" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.foodPicUrl = [data stringValueForKey:@"picurl" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.foodAddress = [data stringValueForKey:@"address" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.foodDesc = [data stringValueForKey:@"desc" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.foodScore = [data intValueForKey:@"score" defaultValue:0];
    retObject.foodPrice = [data intValueForKey:@"price" defaultValue:0];
    CGFloat distance = [data floatValueForKey:@"distance" defaultValue:0.0];
    NSString *distanceStr = nil;
    if (distance > 1000) {
        if (distance/1000 > 999) {
            distanceStr = @"999+km";
        }else{
            distanceStr = [NSString stringWithFormat:@"%.2fkm",distance/1000];
        }
        
    }else{
        distanceStr = [NSString stringWithFormat:@"%dm",(NSInteger)distance];
    }
    retObject.foodDistance = distanceStr;
    return [retObject autorelease];
}

@end

@implementation RCRecommendAllFoodListDataModel

@synthesize total = _total;
@synthesize pn = _pn;
@synthesize rn = _rn;
@synthesize itemListArray = _itemListArray;

-(id)init{
    self = [super init];
    if (self) {
        _itemListArray = nil;
        _total = 0;
    }
    return self;
}

-(void)dealloc{
    self.itemListArray = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    NSDictionary *dict = [data dictionaryValueForKey:@"data" defaultValue:nil];
    if (nil == dict) {
        return nil;
    }
    
    NSArray *dictArray = [dict arrayValueForKey:@"list" defaultValue:nil];
    if (nil == dictArray || [dictArray count] == 0) {
        return nil;
    }
    
    RCRecommendAllFoodListDataModel *retObject = nil;
    retObject = [[RCRecommendAllFoodListDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    
    retObject.total = [dict intValueForKey:@"total" defaultValue:0];
    retObject.pn = [dict intValueForKey:@"pn" defaultValue:0];
    retObject.rn = [dict intValueForKey:@"rn" defaultValue:0];
    
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:[dictArray count]];
    for (int i = 0; i < [dictArray count]; i++) {
        NSDictionary *itemDict = [dictArray safeObjectAtIndex:i];
        if (nil == itemDict) {
            continue;
        }
        RCRecommendFoodListItem *item = [RCRecommendFoodListItem parseDataFromJSON:itemDict];
        if (nil == item) {
            continue;
        }
        [tempArray addObject:item];
    }
    retObject.itemListArray = [NSArray arrayWithArray:tempArray];
    [tempArray release];
    return [retObject autorelease];
}

@end
